package com.zhazha.stand.logic.impl;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.zhazha.stand.entity.Department;
import com.zhazha.stand.logic.DepartmentLogic;
import com.zhazha.stand.mapper.DepartmentMapper;
import com.zhazha.stand.util.MybatisUtil;

public class DepartmentLogicImpl implements DepartmentLogic {

	private Logger logger = Logger.getLogger(DepartmentLogicImpl.class);
	@Override
	public boolean insert(String name) {
		
		SqlSession session  = MybatisUtil.getSession();
		try {
			DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
			logger.info("insert:" + name);
			boolean result = mapper.insert(name);
			session.commit();
			return result;
		} finally {
			MybatisUtil.clossSession();
		}
	}
	@Override
	public boolean insert(Department t) {
		return this.insert(t.getName());
	}

	@Override
	public boolean update(Department t) {
		SqlSession session = MybatisUtil.getSession();
		try {
			DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
			logger.info("delete:" + String.valueOf(t.getId()));
			
			mapper.update(t);
			session.commit();
			return true;
		} finally {
			MybatisUtil.clossSession();
		}
	}

	@Override
	public boolean delete(int id) {
		SqlSession session  = MybatisUtil.getSession();
		try {
			DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
			logger.info("delete:" + String.valueOf(id));
			mapper.delete(id);
			session.commit();
			return true;
		} finally {
			MybatisUtil.clossSession();
		}
	}

	@Override
	public List<Department> getList() {
		SqlSession session  = MybatisUtil.getSession();
		try {
			DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
			logger.info("getlist" );
			
			return mapper.getList();
		} finally {
			MybatisUtil.clossSession();
		}
	}

	@Override
	public Department get(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Department getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	

	
}
